﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LenderClearingHouse
{
    public partial class NewFolderDialog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) return;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            SetFocus(tbFolderName);
        }

        string PropertyPhysicalDir
        {
            get { return Session["PropertyPhysicalDir"].ToString(); }
            set { Session["PropertyPhysicalDir"] = value; }
        }

        string CurrentFoldersPath
        {
            get
            {
                var result = Session["CurrentFoldersPath"] as string;
                if (result == null)
                {
                    result = "";
                    Session["CurrentFoldersPath"] = result;
                }
                return result;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Path.Combine(Path.Combine(PropertyPhysicalDir, CurrentFoldersPath), tbFolderName.Text);
                if (Directory.Exists(path)) throw new NotSupportedException();

                Directory.CreateDirectory(path);
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("CloseAndRefresh();"), true);
            }
            catch (NotSupportedException ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("alert('Folder already exists');"), true);
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                    "key", string.Format("alert('Error creating folder');"), true);
            }
        }
    }
}